package com.borderwaitmaps.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

import com.borderwaitmaps.client.entities.BorderCrossing;
import com.borderwaitmaps.client.entities.VehicleFlow;
import com.borderwaitmaps.client.entities.Location;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mapitz.gwt.googleMaps.client.GClientGeocoder;
import com.mapitz.gwt.googleMaps.client.GControl;
import com.mapitz.gwt.googleMaps.client.GControlAnchor;
import com.mapitz.gwt.googleMaps.client.GControlPosition;
import com.mapitz.gwt.googleMaps.client.GFactualGeocodeCache;
import com.mapitz.gwt.googleMaps.client.GGeoStatusCode;
import com.mapitz.gwt.googleMaps.client.GGeocodeAdvancedResultListener;
import com.mapitz.gwt.googleMaps.client.GGeocodeResult;
import com.mapitz.gwt.googleMaps.client.GIcon;
import com.mapitz.gwt.googleMaps.client.GInfoWindow;
import com.mapitz.gwt.googleMaps.client.GLatLng;
import com.mapitz.gwt.googleMaps.client.GMap2;
import com.mapitz.gwt.googleMaps.client.GMap2Widget;
import com.mapitz.gwt.googleMaps.client.GMarker;
import com.mapitz.gwt.googleMaps.client.GMarkerEventClickListener;
import com.mapitz.gwt.googleMaps.client.GMarkerEventListener;
import com.mapitz.gwt.googleMaps.client.GMarkerEventManager;
import com.mapitz.gwt.googleMaps.client.GMarkerOptions;
import com.mapitz.gwt.googleMaps.client.JSObject;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BorderWaitMaps implements EntryPoint {

	private static BorderWaitMaps singleton;
	public static BorderWaitMaps get()
	{
		return singleton;
	}	
	
	private DockPanel main = new DockPanel();
	private HTML historyFrame = new HTML();
	private GMap2 gmap;
	//private GMap2Widget mapWidget;
	
  public void onModuleLoad() {
	
	singleton = this;
	
    GMap2Widget mapWidget = new GMap2Widget(); 
    gmap = mapWidget.getGmap();    
    
    mapWidget.setHeight("300");
    mapWidget.setWidth("700");
    
    //GLatLng center = new GLatLng(48.864715, -110.251953);
    // for testing, zoom into boundary bay
    GLatLng center = new GLatLng(49.00233309,-123.0680698);
    gmap.setCenter(center, 16);
    
    // add controls
    gmap.addControl(GControl.GMapTypeControl());
    gmap.addControl(GControl.GOverviewMapControl());
    gmap.addControl(GControl.GSmallMapControl());
    gmap.addControl(GControl.GScaleControl());
    mapWidget.setWidth("100%");

    loadCrossings();
    
    main.add(mapWidget, DockPanel.CENTER);
    main.add(historyFrame, DockPanel.SOUTH);
    main.setHeight("90%");
    main.setWidth("100%");
    
    RootPanel.get().add(main);
    
  }

  	private void loadCrossings()
  	{	
  		ArrayList borderCrossings = getBorderCrossings();
  		for (Iterator i = borderCrossings.iterator(); i.hasNext();) {
			
  			BorderCrossing borderCrossing = (BorderCrossing) i.next();
  			addMarker(borderCrossing);
		}
  	}
  	
  	private void addMarker(BorderCrossing borderCrossing)
  	{
  		Location location = borderCrossing.getLocation();
  		double lat = location.getLatitude();
  		double lng = location.getLongitude();
  		GWT.log("lat d=" + lat, null);
  		GWT.log("lng d=" + lng, null);
  		GLatLng coordinates = new GLatLng(lat, lng);
  		
  		/*
  		 * Default color and country
  		 */
  		String country = "can";
  		String color = "green";
  		
  		// determine flag for icon
  		if( borderCrossing.getCountry().equals(BorderCrossing.COUNTRY_CAN) )
  		{
  			country = "can";
  		}
  		else
  		{
  			country = "us";
  		}
  		GWT.log("country="+country, null);
  		
  		
  		/*
  		 * Determine color for icon
  		 * 1. green: 0 < 10 min
  		 * 2. yellow: 11 < 30 min
  		 * 3. red: > 31 min
  		 */
  		String longestDelay = getLongestDelay(borderCrossing);
  		GWT.log("longest delay="+longestDelay, null);
  		Integer longestDelayInteger = new Integer(longestDelay);
  		
  		Integer ten = new Integer(10);
  		Integer thirty = new Integer(30);
  		
  		int ld1 = longestDelayInteger.compareTo(ten);
  		GWT.log("ld1="+ld1, null);
  		
  		int ld2 = longestDelayInteger.compareTo(thirty);
  		GWT.log("ld2="+ld2, null);

  		/*
  		 * If ld1 is less than or equal to 0, color is green
  		 * If ld1 is 1, check ld2
  		 */
  		if( ld1 <= 0 )
  		{
  			color = "green";
  		}
  		else
  		{
  			if( ld2 <= 0 )
  			{
  				color = "yellow";
  			}
  			else
  			{
  				color = "red";
  			}
  		}
  		
  		
  		
  		GMarkerOptions options = new GMarkerOptions();
  		String url = "images/"+country+"_"+color+".png";
  		GWT.log("img url="+url, null);
  		GIcon icon = new GIcon(new GIcon().G_DEFAULT_ICON(), url);
  		options.setIcon(icon);
  		
  		BorderCrossingMarker marker = new BorderCrossingMarker(borderCrossing, coordinates, options);
  		// GMarker marker = new GMarker(coordinates, options);
  		gmap.addOverlay(marker);
  	}
  	
  	private ArrayList getBorderCrossings()
  	{
  		return DataProvider.get().getBorderCrossings();
  	}

	public void getBorderCrossingHistory(BorderCrossing borderCrossing) {
		historyFrame.setText("Display history for: " + borderCrossing.getName());
	}
	
	private String getLongestDelay(BorderCrossing borderCrossing)
	{
		String longestDelay = null;
		
		/*
		 * Get the commercial flow
		 */
		VehicleFlow commercial = borderCrossing.getCommercialVehicleFlow();		
		String com1Delay = commercial.getPrimaryLane().getDelay();
		String com2Delay = commercial.getSecondaryLane().getDelay();
		GWT.log("com1Delay="+com1Delay, null);
		GWT.log("com2Delay="+com2Delay, null);
		
		VehicleFlow passenger = borderCrossing.getPassengerVehicleFlow();
		String pass1Delay = passenger.getPrimaryLane().getDelay();
		String pass2Delay = passenger.getSecondaryLane().getDelay();
		GWT.log("pass1Delay="+pass1Delay, null);
		GWT.log("pass2Delay="+pass2Delay, null);
		
		ArrayList delays = new ArrayList();
		if( com1Delay != null ){delays.add(com1Delay);}
		if( com2Delay != null ){delays.add(com2Delay);}
		if( pass1Delay != null ){delays.add(pass1Delay);}
		if( pass2Delay != null ){delays.add(pass2Delay);}
		
		Collections.sort(delays);
		
		longestDelay = (String) delays.get(delays.size()-1);
		
		return longestDelay;
	}
	
	

}
